package vn.egame.elockscreen.activity;

import vn.egame.elockscreen.ELockscreenPreferences;
import vn.egame.elockscreen.R;
import vn.egame.elockscreen.service.ScreenLockerService;
import vn.egame.elockscreen.tools.HomeWatcher;
import vn.egame.elockscreen.utils.Constans;
import android.app.Application;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Simon.Wu on 2014/7/15.
 */
public class LockApplication extends Application {

	public static HomeWatcher mHomeWatcher;
	public static boolean isLockScreenShowing;
	Handler handler = new Handler();
	Toast toast = null;

	@Override
	public void onCreate() {
		super.onCreate();
		// å¼€å�¯æœ�åŠ¡,å�¯åŠ¨å±�å¹•ç‚¹äº®/å…³é—­ç›‘å�¬
		ELockscreenPreferences.getInstance().initPreferences(
				getApplicationContext());
		if (ELockscreenPreferences.getInstance().isEnableLockscreen()) {
			ScreenLockerService.startActionStart(this, null, null);
		}

		// å±�è”½ç³»ç»Ÿé”�å±�
		// KeyguardManager km = (KeyguardManager)
		// getSystemService(Context.KEYGUARD_SERVICE);
		// KeyguardManager.KeyguardLock kk = km.newKeyguardLock("");
		// kk.disableKeyguard();

		// ç›‘å�¬Homeé”®
		mHomeWatcher = new HomeWatcher(this);
		mHomeWatcher
				.setOnHomePressedListener(new HomeWatcher.OnHomePressedListener() {
					@Override
					public void onHomePressed() {
						Log.e("Kai", "onHomePressed");

						if (isLockScreenShowing) {

							if (toast == null
									|| toast.getView().getWindowVisibility() != View.VISIBLE) {
								toast = Toast
										.makeText(
												getApplicationContext(),
												getApplicationContext()
														.getResources()
														.getString(
																R.string.unlock_by_home),
												Toast.LENGTH_SHORT);
								toast.show();
							}

							// Toast.makeText(
							// getApplicationContext(),
							// getApplicationContext().getResources()
							// .getString(R.string.unlock_by_home),
							// Toast.LENGTH_SHORT).show();

							handler.postDelayed(new Runnable() {

								@Override
								public void run() {
									// TODO Auto-generated method stub
									if (ELockscreenPreferences.getInstance()
											.getLockerPackageTemplate()
											.equals("0")) {
										Intent i = new Intent(
												getApplicationContext(),
												LockScreenDefaultActivity.class)
												.putExtra(
														Constans.START_SCREEN_SAVER_TYPE,
														Constans.StartScreenSaverType.SCREEN_OFF
																.getValue())
												.addFlags(
														Intent.FLAG_ACTIVITY_NEW_TASK);

										startActivity(i);
									} else {
										Intent i = new Intent(
												getApplicationContext(),
												LockScreenAnimationActivity.class)
												.putExtra(
														Constans.START_SCREEN_SAVER_TYPE,
														Constans.StartScreenSaverType.SCREEN_OFF
																.getValue())
												.addFlags(
														Intent.FLAG_ACTIVITY_NEW_TASK);

										startActivity(i);
									}
								}
							}, 2000);

						}

					}

					@Override
					public void onHomeLongPressed() {
						Log.e("Kai", "onHomeLongPressed");
					}
				});
		mHomeWatcher.startWatch();
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		// mHomeWatcher.stopWatch();
		// stopService(new Intent(this, ScreenLockerService.class));
	}

}
